package com.ruoyi.entity;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

public class FileWordCount {
    public static int countWords(MultipartFile file) {
        try {
            // 读取文件内容
            String content = new String(file.getBytes());

            // 替换标点符号和空格
            String cleanedContent = content.replaceAll("[^a-zA-Z0-9 ]", "");

            // 按空格分割字符串得到单词数组
            String[] words = cleanedContent.split(" ");

            // 返回单词数量
            return words.length;
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
    }
}
